package com.xiaoxu.auth.utils;

import com.xiaoxu.model.system.SysMenu;

import java.util.ArrayList;
import java.util.List;

public class MenuHelper {

    public static List<SysMenu> buildTree(List<SysMenu> sysMenuList){

        //封装最后的对象
        List<SysMenu> trees = new ArrayList<>();
        //程序的入口
        for (SysMenu sysMenu : sysMenuList) {
            if (sysMenu.getParentId() == 0) {
                trees.add(findChildren(sysMenu, sysMenuList));
            }
        }
        return trees;
    }


    public static SysMenu findChildren(SysMenu sysMenu,List<SysMenu> sysMenuList){
        //构造子菜单的数据
        sysMenu.setChildren(new ArrayList<SysMenu>());
        for (SysMenu item: sysMenuList){
            if (item.getParentId() == sysMenu.getId()){

                if (sysMenu.getChildren() == null) {
                    sysMenu.setChildren(new ArrayList<>());
                }
                sysMenu.getChildren().add(findChildren(item,sysMenuList));

            }

        }

        return sysMenu;
    }
}
